
export default class ModelUtil {
    constructor(viewer){
        this.viewer =viewer;
        this.tileset =undefined;
        this.geometry =undefined;
        this.model =undefined;
    }
    loadData({position={lng:0,lat:0,alt:0},hpr={heading:0,pitch:0,roll:0},modelUrl=""}){
        let orgin =Cesium.Cartesian3.fromDegrees(position.lng,position.lat,position.alt);
        let headingPitchRoll =new Cesium.HeadingPitchRoll(hpr.heading,hpr.pitch,hpr.roll);
        let modelMatrix =Cesium.Transforms.headingPitchRollToFixedFrame(orgin,headingPitchRoll);
        let model =scene.primitives.add(
            Cesium.Model.fromGltf({
                url:modelUrl,
                modelMatrix:modelMatrix,
                minimumPixelSize:128,
            })
        );
        model.readyPromise.then((model)=>{
            this.viewer.zoomTo(model);
        })
    }
    loadTiles(option){
        this.tileset =new Cesium.Cesium3DTileset(
            option
        );
        this.tileset.readyPromise.then((tileset)=>{
            this.viewer.scene.primitives.add(tileset);
            this.viewer.zoomTo(tileset,new Cesium.HeadingPitchRange(
                0.0,
                -0.5,
                tileset.boundingSphere.radius * 2.0
                )
            );
            if(option.height){
                this.setHeight(option.height);
            }
            if(option.style){
                this.tileset.style=option.style;
            }
        }).otherwise(function(error){console.log(error);});

    }

    loadModel(options){
        let viewer =this.viewer;
        let position =options.position;
        let orientation =options.orientation || {heading:0,pitch:0,roll:0};
        let scale =options.scale || 1.0;    
        if(options.url){
            let pos =new Cesium.Cartesian3.fromDegrees(position.lon,position.lat,position.height);
            let ort =Cesium.Transforms.headingPitchRollQuaternion(pos,
                new Cesium.HeadingPitchRoll(orientation.heading,orientation.pitch,orientation.roll));
            this.model =viewer.entities.add({
                position: pos,
                orientation: ort,
                model:{
                    uri :options.url,
                    scale: scale
                },
                properties: ""
            });
            viewer.zoomTo(this.model);
            }
    }
    remove(){
        if(this.geometry){
            this.viewer.entities.remove(this.geometry);
        }
        if(this.model){
            this.viewer.entities.remove(this.model)
        }
        if(this.tileset){
            this.viewer.scene.primitives.remove(this.tileset)
        }
    }
    setHeight(height){
        let tileset =this.tileset;
        let cartographic =Cesium.Cartographic.fromCartesian(tileset.boundingSphere.center);
        let surface =Cesium.Cartesian3.fromRadians(cartographic.longitude,cartographic.latitude,0.0);
        let offset =Cesium.Cartesian3.fromRadians(cartographic.longitude,cartographic.latitude,height);
        let translation =Cesium.Cartesian3.subtract(offset,surface,new Cesium.Cartesian3);
        tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation);
    }
    loadGeometry(option){
        this.geometry =new Cesium.Entity(option);
        this.viewer.entities.add(this.geometry);
        this.viewer.zoomTo(this.geometry);  
    }
    loadOpenCylinder(){
        this.geometry =new Cesium.Entity({
            name: "Green extruded Circle",
            polygon: {
              hierarchy: Cesium.Cartesian3.fromDegreesArray([
                -108.0,
                42.0,
                -100.0,
                42.0,
                -104.0,
                40.0,
              ]),
              extrudedHeight: 500000.0,
              material: Cesium.Color.GREEN,
              closeTop: false,
              closeBottom: false,
            },
          });
        this.viewer.entities.add(this.geometry);
           
          console.log(this.viewer.entities);
    }
    loadRedCone(){
        this.remove();
        this.geometry =new Cesium.Entity({
            name: "Red cone",
            position: Cesium.Cartesian3.fromDegrees(-105.0, 40.0, 200000.0),
            cylinder: {
              length: 400000.0,
              topRadius: 0.0,
              bottomRadius: 200000.0,
              material: Cesium.Color.RED,
            },
          })
          this.viewer.entities.add(this.geometry);
          this.viewer.zoomTo(this.geometry);  
          console.log(this.viewer.entities);
    }
}